<template>
    <div class="area">
      <div class="context" :style="{ top: isConsultant ? '0' : '32.5vh' }">
        <h1 v-show="!isConsultant">您的身份是?</h1>
        <div class="button-container" v-show="!isConsultant">
            <button @click="switchToConsultant">我是咨询师</button>
        </div>
    
  
        <h1 v-show="isConsultant" style="margin-top: 50px;">咨询师</h1>
        <transition name="slide">
            <login v-show="isConsultant" @login="handleLogin" @back="switchToAdmin"/>
        </transition>
      </div>
  
      <ul class="circles">
        <li v-for="n in 20" :key="n"></li>
      </ul>
    </div>
  </template>
  
  <script setup>
  import { ref, defineEmits, onMounted } from 'vue';
  import { useRouter } from 'vue-router'; // 引入 useRouter
  import login from './login/login.vue';
  
  const isConsultant = ref(false);
  const emit = defineEmits(['updateSpan']);
  
  const router = useRouter(); // 获取路由实例
  
  function switchToConsultant() {
    isConsultant.value = true;
    emit('updateSpan', 'consultant');
  }
  
  function switchToAdmin() {
    isConsultant.value = false;
    emit('updateSpan', 'reset');
  }
  
  // 处理登录逻辑
  async function handleLogin(loginData) {
    try {
      // 构建查询参数
      const queryParams = new URLSearchParams({
          username: loginData.username,
          password: loginData.password,
      }).toString();
  
      // 发起请求
      const response = await fetch(`/api/hqy/consultant/login?${queryParams}`, {
        method: 'POST',
      });
  
      const res = await response.json();
  
      
      if (res.code === 200) {
        // 登录成功，将用户信息存储到 sessionStorage
        sessionStorage.setItem('consultant', JSON.stringify(res.data));
        alert('Login success!');
        // 跳转到首页或其他页面
        router.push('/layout1'); // 使用 router.push 进行跳转

      } else {
        // 登录失败，显示错误信息
        alert(`Login failed: ${res.message}`);
      }
    } catch (error) {
      console.error('Login request failed:', error);
      alert('Login request failed. Please try again later.');
    }
  }
  
  onMounted(() => {
    console.log('Animated Background component mounted');
  });
  </script>
  
  <style lang="scss" scoped>
  @import url('https://fonts.googleapis.com/css?family=Exo:400,700');
  
  * {
    margin: 0px;
    padding: 0px;
  }
  
  body {
    font-family: 'Exo', sans-serif;
  }
  
  .context {

    height: 65vh;
    width: 100%;
    margin: 0;
    padding: 0;
    position: absolute;
    z-index: 999;

  }
  
  .context h1 {
    text-align: center;
    color: #fff;
    font-size: 50px;
  }
  
  .area {
    background: #4e54c8;
    background: -webkit-linear-gradient(to left, #8f94fb, #4e54c8);
    width: 100%;
    height: 115vh;
    position: relative;
  }
  
  .circles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  
  .circles li {
    position: absolute;
    display: block;
    list-style: none;
    width: 20px;
    height: 20px;
    background: rgba(255, 255, 255, 0.2);
    animation: animate 25s linear infinite;
    bottom: -150px;
  }
  
  /* 增加更多的方块样式 */
  .circles li:nth-child(1) { left: 5%; width: 60px; height: 60px; animation-delay: 0s; }
  .circles li:nth-child(2) { left: 10%; width: 20px; height: 20px; animation-delay: 2s; animation-duration: 12s; }
  .circles li:nth-child(3) { left: 15%; width: 30px; height: 30px; animation-delay: 4s; }
  .circles li:nth-child(4) { left: 20%; width: 40px; height: 40px; animation-delay: 0s; animation-duration: 18s; }
  .circles li:nth-child(5) { left: 25%; width: 20px; height: 20px; animation-delay: 0s; }
  .circles li:nth-child(6) { left: 30%; width: 70px; height: 70px; animation-delay: 3s; }
  .circles li:nth-child(7) { left: 35%; width: 50px; height: 50px; animation-delay: 7s; }
  .circles li:nth-child(8) { left: 40%; width: 25px; height: 25px; animation-delay: 15s; animation-duration: 45s; }
  .circles li:nth-child(9) { left: 45%; width: 15px; height: 15px; animation-delay: 2s; animation-duration: 35s; }
  .circles li:nth-child(10) { left: 50%; width: 80px; height: 80px; animation-delay: 0s; animation-duration: 11s; }
  .circles li:nth-child(11) { left: 55%; width: 35px; height: 35px; animation-delay: 6s; }
  .circles li:nth-child(12) { left: 60%; width: 45px; height: 45px; animation-delay: 1s; animation-duration: 22s; }
  .circles li:nth-child(13) { left: 65%; width: 20px; height: 20px; animation-delay: 9s; }
  .circles li:nth-child(14) { left: 70%; width: 90px; height: 90px; animation-delay: 5s; }
  .circles li:nth-child(15) { left: 75%; width: 65px; height: 65px; animation-delay: 8s; }
  .circles li:nth-child(16) { left: 80%; width: 22px; height: 22px; animation-delay: 14s; animation-duration: 38s; }
  .circles li:nth-child(17) { left: 85%; width: 18px; height: 18px; animation-delay: 3s; animation-duration: 28s; }
  .circles li:nth-child(18) { left: 90%; width: 75px; height: 75px; animation-delay: 10s; animation-duration: 16s; }
  .circles li:nth-child(19) { left: 95%; width: 40px; height: 40px; animation-delay: 12s; }
  .circles li:nth-child(20) { left: 30%; width: 55px; height: 55px; animation-delay: 11s; animation-duration: 20s; }
  
  @keyframes animate {
    0% {
      transform: translateY(0) rotate(0deg);
      opacity: 1;
      border-radius: 0;
    }
  
    100% {
      transform: translateY(-1000px) rotate(720deg);
      opacity: 0;
      border-radius: 50%;
    }
  }
  
  .button-container {
    margin-top: 20px;
    text-align: center;
  
    button {
      margin: 0 10px;
      margin-right: 15px;
      padding: 8px 16px;
      font-size: 30px;
      cursor: pointer;
      background-color: #04051b;
      color: white;
      border: none;
      border-radius: 4px;
      transition: background-color 0.3s;
    }
  
    button:hover {
      background-color: #8f94fb;
    }
  }

    /* 重新设计的过渡动画 */
    .slide-enter-active {
    animation: slideIn 0.5s forwards;
    }

    .slide-leave-active {
    animation: slideOut 0.5s forwards;
    }

    @keyframes slideIn {
    from {
        width: 0%;
        opacity: 0;
    }
    to {
        width: 400px;
        opacity: 1;
    }
    }

    @keyframes slideOut {
    from {
        width: 400px;
        opacity: 1;
    }
    to {
        width: 15%;
        opacity: 0;
    }
}
  </style>